*IAT SCORING ALGORITHM FROM GREENWALD, NOSEK, & BANAJI, 2002
LAST UPDATE: 03/03/04

This macro will transform a datafile with raw trial latencies (stored as one line per response) 
for a standard format IAT (7 blocks) into a one line summary per subject of the IAT effect using
GNB's new scoring algorithm.

To use this algorithm for your SAS program, perform these two steps:  
(1) Run this script.  The macro should then be loaded into SAS's active memory.  
(2) In your SAS program, enter the following statement 

%iatCalc(libIn, libOut, indata, outdata, BLOCNAME, SESHID, TRLLATNC, TRLERROR, VERROR, VEXTREME, VSD)

and replace the dummy values with the ones that correspond to your own datafiles and variable names.  


Descriptions of what the macro expects for input, and what it will output are below.  Also, at the 
bottom of this commenting section, there is a sample SAS script that would invoke this macro (assuming 
that you have already run the macro to get it into SAS's active memory).

   The macro expects the following types of libraries, datafiles, and data:
      libIn     = input SAS library name
      libOut    = output SAS library name
      indata    = filename of input SAS dataset in the input SAS library
      outdata   = filename for the output SAS dataset in the output SAS library
      BLOCNAME  = variable name for block identifier in the indata file: alphanumeric indication of the 
                  four trial blocks ('B3', 'B4', 'B6', 'B7' are critical blocks corresponding 
                  to B3, B4, B6, and B7 from GNB, 2002).  At present the macro requires that the variable 
                  passed here uses the names 'B3', 'B4', 'B6', 'B7' to refer to the B3-B7 blocks
	  SESHID    = variable name for unique subject identifier in the indata file
	  TRLLATNC  = variable name for latency of response for trial in the indata file
	  TRLERROR  = variable name for error coding: 0 if initial response was correct, 1 if initial response 
                  was incorrect in the indata file
      VERROR    = value: if '1' the algorithm will use error trial latencies, if '2' the algorithm will replace 
                  error trial latencies with blockmean+600
      VEXTREME  = value: if '1' the algorithm provides no treatment of extreme values, if '2' the algorithm
                  will delete trials <400ms
      VSTD      = value: if '1' the block standard deviation is performed including corrected error trials,
                  if '2' the block standard deviation is performed on correct responses only (1 is standard)

   The macro will output the following variables to the outdata file:
      SESHID  = unique subject identifier
      SUBEXCL = 0 for inclusion data, 1 for excluded data, 2 for incomplete data
      MB3     = mean of trial latencies for B3
      MB4     = mean of trial latencies for B4
      MB6     = mean of trial latencies for B6
      MB7     = mean of trial latencies for B7
      CS1     = standard deviation for B3 and B6 trials combined (correct trials only)
      CS2     = standard deviation for B4 and B7 trials combined (correct trials only)
      AS1     = standard deviation for B3 and B6 trials combined (all trials)
      AS2     = standard deviation for B4 and B7 trials combined (all trials)
      EB3     = percent errors of trials for B3
      EB4     = percent errors of trials for B4
      EB6     = percent errors of trials for B6
      EB7     = percent errors of trials for B7
      FB3     = percent fast responses of trials for B3
      FB4     = percent fast responses of trials for B4
      FB6     = percent fast responses of trials for B6
      FB7     = percent fast responses of trials for B7
      DIFF1   = MB6 - MB3
      DIFF2   = MB7 - MB4
      IAT1    = DIFF1/STD1
      IAT2    = DIFF2/STD2
	  IAT     = mean of IAT1 and IAT2
;

/* SAMPLE SAS SCRIPT THAT USES THIS MACRO ASSUMING THAT IT IS IN ACTIVE SAS MEMORY
This sample script opens two libraries called 'web' and 'outdata'. In the 'web' library a SAS file 
named 'iatrace350000' already exists and will be pulled into active memory by the macro.  When the macro 
finishes it will output the result data into a file in the library 'outdata' called 'Ciatrace350000'. The 
information coding the name of the block is in the variable BLOCK (which is passed to 'BLOCNAME' for
the algorithm). The information coding subject ID is SUB (passed to 'SESHID').  The information coding
the latency of each trial is in LATENCY (passed to TRLLATNC).  The information coding whether an error
was made on each trial is in ERROR (passed to TRLERROR).  And, the setting '2' for VERROR means that 
error latencies will be replaced with the blockmean+600.  The setting '2' for VEXTREME means that responses
faster than 400ms will be deleted before calculating the IAT effect. The setting '1' for VSTD means that 
the standard deviations will be calculated after recombining error trials so the range of D values is bounded 
between (-2,2).  When the algorithm finishes and saves the output, the proc means statement will calculate a 
mean of the IAT effects across subjects (after excluding 'bad' subjects).

libname web 'H:\raceatt\';
libname outdata 'H:\raceattclean\';
%iatCalc(web, outdata, iatrace350000, Ciatrace350000, BLOCK, SUB, LATENCY, ERROR, 2, 2,1);
proc means (data=outdata.Ciatrace350000);var IAT;where SUBEXCL=0;run;
*/

/*MACROS for NEW IMPLICIT ASSOCIATION TEST SCORING ALGORITHM (Greenwald, Nosek, & Banaji, 2002)*/
%macro iatCalc(libIn, libOut, indata, outdata, BLOCNAME, SESHID, TRLLATNC, TRLERROR, VERROR, VEXTREME, VSTD);
%let divide= /; %let multiply= *; %let add = +; %let subtract = -;
%iatAlgorithm(libIn=&libIn, libOut=&libOut, indata=&indata, outdata=&outdata, BLOCNAME=&BLOCNAME, 
              SESHID=&SESHID, TRLLATNC=&TRLLATNC, TRLERROR=&TRLERROR, VERROR=&VERROR, VEXTREME=&VEXTREME, VSTD=&VSTD);run;
%mend iatCalc;

%macro iatAlgorithm(libIn, libOut, indata, outdata, BLOCNAME, SESHID, TRLLATNC, TRLERROR, VERROR, VEXTREME, VSTD);
data IAT; set &libIn..&indata; 
%*PRELIMINARY STEPS FOR HANDLING WEBDATA FORMATS;
    keep &BLOCNAME &SESHID &TRLLATNC &TRLERROR;
proc sort data=iat; by &SESHID &BLOCNAME;	
	%*options nonotes; %*suppress all Notes to log;	

data IAT; set IAT;

%*STEP 1: Include data from B3, B4, B6, B7;
   if &BLOCNAME in ('B3', 'B4', 'B6', 'B7') then ;
   else delete;

%*STEP 2a: Eliminate trial latencies > 10,000ms;
   if &TRLLATNC > 10000 then delete;

%*STEP 2b: Eliminate subjects for whom more than 10% of trials have latencies < 300ms;
   else if &TRLLATNC < 0 then delete; %*for miscoded data in datafile indicating negative response times;
   else if -1 < &TRLLATNC < 300 then FAST = 1;
   else FAST = 0;

data FASTDATA; set IAT; keep &SESHID &BLOCNAME FAST;
proc sort; by &SESHID &BLOCNAME;
proc means data=IAT noprint; by &SESHID &BLOCNAME; var FAST; output out=means mean=MEAN;
proc transpose data=means prefix=F name=name out=FASTMEAN; by &SESHID; id &BLOCNAME;
data FASTMEAN; set FASTMEAN; where name='MEAN'; FASTM = mean(FB3, FB4, FB6, FB7);
   if FASTM > .10 then SUBEXCL = 1; else SUBEXCL = 0; 
   %*SUBEXCL = 0 (include data), 1 (exclude data - too many fast responses), 2 (exclude data - missing data);
   %*The SUBEXCL variable needed to be reintroduced to the final dataset in STEP 12;

%*STEP 3: Use all trials;
   %*in the conventional algorithm the first two trials of each block would be dropped here;

%*STEP 4: No extreme value treatment <or> delete trial with latencies <400ms;
data IAT; set IAT; 
    %*if &VEXTREME = 1 then do nothing here;
    if &VEXTREME = 2 then do; if &TRLLATNC < 400 then delete; end;

proc sort data=IAT; by &SESHID &BLOCNAME;

%*STEP 5: Compute mean of correct latencies for each block;
data CORR; set IAT;
   %*if &VERROR is 1 then means and SDs will be calculated for the entire set of latencies;
   if &VERROR = 2 then do; if &TRLERROR NE 0 then delete; end;
   keep &SESHID &BLOCNAME &TRLLATNC &TRLERROR;
proc means data=CORR noprint; by &SESHID &BLOCNAME; var &TRLLATNC; output out=means mean=MEAN;
proc transpose data=means prefix=M name=name out=CORRMEAN; by &SESHID; id &BLOCNAME;
data CORRMEAN; set CORRMEAN; where name='MEAN';

%*STEP 6a: Compute pooled SD for B3 & B6, and separately for B4 & B7 for correct trials only;
data SD; set CORR;
   if &TRLERROR NE 0 then delete;  *drop error trials;
   if &BLOCNAME in ('B3', 'B6') then TD = '1';
   else if &BLOCNAME in ('B4', 'B7') then TD = '2';
   else delete;
   drop &BLOCNAME;
proc sort data=SD; by &SESHID TD;
proc means data=SD noprint; by &SESHID TD; var &TRLLATNC; output out=means std=STD;
proc transpose data=means prefix=CS name=name out=CORRSTD2; by &SESHID; id TD;
data CORRSTD2; set CORRSTD2; where name='STD';

%*STEP 6b: Compute pooled SD for B3 & B6, and separately for B4 & B7 including error trials;
data SD; merge IAT CORRMEAN; by &SESHID;
   if &TRLERROR < 0 then delete;
   else if &TRLERROR > 1 then delete; %*get rid of coding errors;
   else if &TRLERROR = 1 and &VERROR = 2 then do;
      if &BLOCNAME in ('B3') then &TRLLATNC = MB3 + 600;
	  else if &BLOCNAME in ('B4') then &TRLLATNC = MB4 + 600;
	  else if &BLOCNAME in ('B6') then &TRLLATNC = MB6 + 600;
	  else if &BLOCNAME in ('B7') then &TRLLATNC = MB7 + 600;
   end;
   if &BLOCNAME in ('B3', 'B6') then TD = '1';
   else if &BLOCNAME in ('B4', 'B7') then TD = '2';
   else delete;
   drop &BLOCNAME;
proc sort data=SD; by &SESHID TD;
proc means data=SD noprint; by &SESHID TD; var &TRLLATNC; output out=means std=STD;
proc transpose data=means prefix=AS name=name out=CORRSTD1; by &SESHID; id TD;
data CORRSTD1; set CORRSTD1; where name='STD';

%*STEP 7: Replace error latencies with block mean + 600ms 
<or> use latency from stimulus onset to correct response (when correct response is required);
data ERR; set IAT;
   keep &SESHID &BLOCNAME &TRLERROR;
   if &TRLERROR < 0 then delete;
   else if &TRLERROR > 1 then delete; %*get rid of coding errors;
proc means data=ERR noprint; by &SESHID &BLOCNAME; var &TRLERROR; output out=means mean=MEAN;
proc transpose data=means prefix=E name=name out=ERRMEAN; by &SESHID; id &BLOCNAME;
data ERRMEAN; set ERRMEAN; where name='MEAN';

%*STEP 7 continued: combining data;
data COMBINE; merge CORRMEAN CORRSTD1 CORRSTD2 ERRMEAN FASTMEAN; by &SESHID; %*combining datasets for calculating final means;
   if &VERROR=2 then do;
      array BLOCKMeans(*) MB: ;
      array BLOCKCstds(*) CS: ;
	  array BLOCKAstds(*) AS: ;
      array BLOCKErrs(*) EB: ;
      do i=1 to dim(BLOCKMeans); %*for each of the four blocks replace error trials with mean + 600ms;
	     BLOCKMeans{i} = (1-BLOCKErrs{i})*BLOCKMeans{i} + (BLOCKErrs{i})*(BLOCKMeans{i}+600);			
      end;
   end;

%*STEP 8: No transformation of latencies;
    %*in the conventional algorithm, raw latencies would be log transformed prior to the transposing in the current format;

%*STEP 9: Average latencies for each of the four blocks;
    %*this step was already accomplished in the do loop above;

%*STEP 10: Compute two differences B6-B3 and B7-B4 (does not account for pairing order);
   DIFF1 = MB6 - MB3;
   DIFF2 = MB7 - MB4;

%*STEP 11: Divide each difference by associated pooled SD from STEP 6a or 6b;
   If &VSTD = 2 then do;
      IAT1 = DIFF1/CS1;
      IAT2 = DIFF2/CS2;
   end;
   else do; %*IF VSTD = 1 also set as default;
      IAT1 = DIFF1/AS1;
	  IAT2 = DIFF2/AS2;
   end; 

%*STEP 12: Average quotients from STEP 11;
   IAT = mean(IAT1, IAT2);

%*if there is missing data, do mark data as excluded (SUBEXCL=2);
   do i=1 to dim(BLOCKMeans);
      if BLOCKMeans{i} = . then SUBEXCL = 2;
   end;
data &libout..&outdata (drop=i name);
	set COMBINE;
run;
%mend iatAlgorithm;
*END OF ALGORITHM;

libname study 'C:\primary\Data\Dissertation.2001\Diss.3\';
data comb1;
  informat Code $18.;informat Bname $18.;infile 'C:\primary\Data\Dissertation.2001\Diss.3\diss.study3.to68.dat' delimiter='09'x;
  input date    time $ build sub     code $  block   trial   lat     resp    corr    stim1 bname $;
data comb2;
  informat Code $18.;informat Bname $18.;infile 'C:\primary\Data\Dissertation.2001\Diss.3\diss3.study3.2.dat' delimiter='09'x;
  input date    time $ build sub     code $  block   trial   lat     resp    corr    stim1 bname $;
data comb3;
  informat Code $18.;informat Bname $18.;infile 'C:\primary\Data\Dissertation.2001\Diss.3\diss3.3.txt' delimiter='09'x;
  input date    time $ build sub     code $  block   trial   lat     resp    corr    stim1 bname $;
data comb4;
  informat Code $18.;informat Bname $18.;infile 'C:\primary\Data\Dissertation.2001\Diss.3\diss3.4.txt' delimiter='09'x;
  input date    time $ build sub     code $  block   trial   lat     resp    corr    stim1 bname $;
data comb; set comb1 comb2 comb3 comb4;
  drop build time date; 
  if resp = 16 then delete; /*control quit*/
  err = 1 - corr;

  if bname in ('bcode', 'boggle1', 'boggle2', 'boggle3', 'interview', 'badgood', 'flowersinsects', 'insectsflowers',
               '110900', '111400', '111500', '111600', '111700') then delete;

/*proc means;class bname; var lat;run;
proc means; class code;var lat;run;
proc means; class sub; var corr;run;
proc means mean std; class sub bname; var corr;run;
proc means; class block;var lat;run;*/

proc sort; by sub;
data study.raw; set comb;

data study.iFI; set study.raw;
  if bname = 'flowersgood' and trial < 22 then blockn = 'B3';
  else if bname = 'flowersgood' and trial > 22 then blockn = 'B4';
  else if bname = 'flowersbad' and trial < 22 then blockn = 'B6';
  else if bname = 'flowersbad' and trial > 22 then blockn = 'B7';
  else delete;
%iatCalc(study, study, iFI, iFIclean, blockn, SUB, LAT, err, 2, 2, 1);
data study.iFIclean; set study.iFIclean;
  FIiat = IAT; FIiat1 = IAT1; FIiat2 = IAT2;
  FIiatexcl = subexcl; FItradIAT = DIFF2;
  FImb3 = mb3; FImb4 = mb4; FImb6 = mb6; FImb7 = mb7; 
  FIeb3 = eb3; FIeb4 = eb4; FIeb6 = eb6; FIeb7 = eb7; 
  if FIeb3>.39 | FIeb4>.39 | FIeb6>.39 | FIeb7>.39 then FIiatexcl=1;
  if FIiatexcl > 0 then do; FIiat = .; FIiat1 = .; FIiat2 = .; FItradIAT = .; end;
  drop IAT IAT1 IAT2 subexcl mb3 mb4 mb6 mb7 CS1 CS2 AS1 AS2 EB3 EB4 EB6 EB7 FB3 FB4 FB6 FB7 FASTM DIFF1 DIFF2;
*proc means;run;

data study.iDR; set study.raw;
  if bname = 'democratgood' and trial < 22 then blockn = 'B3';
  else if bname = 'democratgood' and trial > 22 then blockn = 'B4';
  else if bname = 'democratbad' and trial < 22 then blockn = 'B6';
  else if bname = 'democratbad' and trial > 22 then blockn = 'B7';
  else delete;
%iatCalc(study, study, iDR, iDRclean, blockn, SUB, LAT, err, 2, 2, 1);
data study.iDRclean; set study.iDRclean;
  DRiat = IAT; DRiat1 = IAT1; DRiat2 = IAT2;
  DRiatexcl = subexcl; DRtradIAT = DIFF2;
  DRmb3 = mb3; DRmb4 = mb4; DRmb6 = mb6; DRmb7 = mb7; 
  DReb3 = eb3; DReb4 = eb4; DReb6 = eb6; DReb7 = eb7; 
  if DReb3>.39 | DReb4>.39 | DReb6>.39 | DReb7>.39 then DRiatexcl=1;
  if DRiatexcl > 0 then do; DRiat = .; DRiat1 = .; DRiat2 = .; DRtradIAT = .; end;
  drop IAT IAT1 IAT2 subexcl mb3 mb4 mb6 mb7 CS1 CS2 AS1 AS2 EB3 EB4 EB6 EB7 FB3 FB4 FB6 FB7 FASTM DIFF1 DIFF2;
*proc means;run;

data study.iGS; set study.raw;
  if bname = 'gaybad' and trial < 22 then blockn = 'B3';
  else if bname = 'gaybad' and trial > 22 then blockn = 'B4';
  else if bname = 'gaygood' and trial < 22 then blockn = 'B6';
  else if bname = 'gaygood' and trial > 22 then blockn = 'B7';
  else delete;
%iatCalc(study, study, iGS, iGSclean, blockn, SUB, LAT, err, 2, 2, 1);
data study.iGSclean; set study.iGSclean;
  GSiat = IAT; GSiat1 = IAT1; GSiat2 = IAT2;
  GSiatexcl = subexcl; GStradIAT = DIFF2;
  GSmb3 = mb3; GSmb4 = mb4; GSmb6 = mb6; GSmb7 = mb7; 
  GSeb3 = eb3; GSeb4 = eb4; GSeb6 = eb6; GSeb7 = eb7; 
  if GSeb3>.39 | GSeb4>.39 | GSeb6>.39 | GSeb7>.39 then GSiatexcl=1;
  if GSiatexcl > 0 then do; GSiat = .; GSiat1 = .; GSiat2 = .; GStradIAT = .; end;
  drop IAT IAT1 IAT2 subexcl mb3 mb4 mb6 mb7 CS1 CS2 AS1 AS2 EB3 EB4 EB6 EB7 FB3 FB4 FB6 FB7 FASTM DIFF1 DIFF2;
*proc means;run;

data study.iBW; set study.raw;
  if bname = 'blackbad' and trial < 22 then blockn = 'B3';
  else if bname = 'blackbad' and trial > 22 then blockn = 'B4';
  else if bname = 'blackgood' and trial < 22 then blockn = 'B6';
  else if bname = 'blackgood' and trial > 22 then blockn = 'B7';
  else delete;
%iatCalc(study, study, iBW, iBWclean, blockn, SUB, LAT, err, 2, 2, 1);
data study.iBWclean; set study.iBWclean;
  BWiat = IAT; BWiat1 = IAT1; BWiat2 = IAT2;
  BWiatexcl = subexcl; BWtradIAT = DIFF2;
  BWmb3 = mb3; BWmb4 = mb4; BWmb6 = mb6; BWmb7 = mb7; 
  BWeb3 = eb3; BWeb4 = eb4; BWeb6 = eb6; BWeb7 = eb7; 
  if BWeb3>.39 | BWeb4>.39 | BWeb6>.39 | BWeb7>.39 then BWiatexcl=1;
  if BWiatexcl > 0 then do; BWiat = .; BWiat1 = .; BWiat2 = .; BWtradIAT = .; end;
  drop IAT IAT1 IAT2 subexcl mb3 mb4 mb6 mb7 CS1 CS2 AS1 AS2 EB3 EB4 EB6 EB7 FB3 FB4 FB6 FB7 FASTM DIFF1 DIFF2;
*proc means;run;

data study.iSH; set study.raw;
  if bname = 'sciencebad' and trial < 22 then blockn = 'B3';
  else if bname = 'sciencebad' and trial > 22 then blockn = 'B4';
  else if bname = 'sciencegood' and trial < 22 then blockn = 'B6';
  else if bname = 'sciencegood' and trial > 22 then blockn = 'B7';
  else delete;
%iatCalc(study, study, iSH, iSHclean, blockn, SUB, LAT, err, 2, 2, 1);
data study.iSHclean; set study.iSHclean;
  SHiat = IAT; SHiat1 = IAT1; SHiat2 = IAT2;
  SHiatexcl = subexcl; SHtradIAT = DIFF2;
  SHmb3 = mb3; SHmb4 = mb4; SHmb6 = mb6; SHmb7 = mb7; 
  SHeb3 = eb3; SHeb4 = eb4; SHeb6 = eb6; SHeb7 = eb7; 
  if SHeb3>.39 | SHeb4>.39 | SHeb6>.39 | SHeb7>.39 then SHiatexcl=1;
  if SHiatexcl > 0 then do; SHiat = .; SHiat1 = .; SHiat2 = .; SHtradIAT = .; end;
  drop IAT IAT1 IAT2 subexcl mb3 mb4 mb6 mb7 CS1 CS2 AS1 AS2 EB3 EB4 EB6 EB7 FB3 FB4 FB6 FB7 FASTM DIFF1 DIFF2;
*proc means;run;

data study.iCE; set study.raw;
  if bname = 'creationgood' and trial < 22 then blockn = 'B3';
  else if bname = 'creationgood' and trial > 22 then blockn = 'B4';
  else if bname = 'creationbad' and trial < 22 then blockn = 'B6';
  else if bname = 'creationbad' and trial > 22 then blockn = 'B7';
  else delete;
%iatCalc(study, study, iCE, iCEclean, blockn, SUB, LAT, err, 2, 2, 1);
data study.iCEclean; set study.iCEclean;
  CEiat = IAT; CEiat1 = IAT1; CEiat2 = IAT2;
  CEiatexcl = subexcl; CEtradIAT = DIFF2;
  CEmb3 = mb3; CEmb4 = mb4; CEmb6 = mb6; CEmb7 = mb7; 
  CEeb3 = eb3; CEeb4 = eb4; CEeb6 = eb6; CEeb7 = eb7; 
  if CEeb3>.39 | CEeb4>.39 | CEeb6>.39 | CEeb7>.39 then CEiatexcl=1;
  if CEiatexcl > 0 then do; CEiat = .; CEiat1 = .; CEiat2 = .; CEtradIAT = .; end;
  drop IAT IAT1 IAT2 subexcl mb3 mb4 mb6 mb7 CS1 CS2 AS1 AS2 EB3 EB4 EB6 EB7 FB3 FB4 FB6 FB7 FASTM DIFF1 DIFF2;
*proc means;run;

data study.iTF; set study.raw;
  if bname = 'fatbad' and trial < 22 then blockn = 'B3';
  else if bname = 'fatbad' and trial > 22 then blockn = 'B4';
  else if bname = 'fatgood' and trial < 22 then blockn = 'B6';
  else if bname = 'fatgood' and trial > 22 then blockn = 'B7';
  else delete;
%iatCalc(study, study, iTF, iTFclean, blockn, SUB, LAT, err, 2, 2, 1);
data study.iTFclean; set study.iTFclean;
  TFiat = IAT; TFiat1 = IAT1; TFiat2 = IAT2;
  TFiatexcl = subexcl; TFtradIAT = DIFF2;
  TFmb3 = mb3; TFmb4 = mb4; TFmb6 = mb6; TFmb7 = mb7; 
  TFeb3 = eb3; TFeb4 = eb4; TFeb6 = eb6; TFeb7 = eb7; 
  if TFeb3>.39 | TFeb4>.39 | TFeb6>.39 | TFeb7>.39 then TFiatexcl=1;
  if TFiatexcl > 0 then do; TFiat = .; TFiat1 = .; TFiat2 = .; TFtradIAT = .; end;
  drop IAT IAT1 IAT2 subexcl mb3 mb4 mb6 mb7 CS1 CS2 AS1 AS2 EB3 EB4 EB6 EB7 FB3 FB4 FB6 FB7 FASTM DIFF1 DIFF2;
*proc means;run;

data compexp;  informat race $10.; informat major $15.;
  infile 'C:\primary\Data\Dissertation.2001\Diss.3\explicit.data.diss.3.txt' delimiter='09'x lrecl=1500;
  input sub bgc $ bgn bg1-bg20 cwc $ cwn cw1-cw20 npc $ npn np1-np20 ufc $ ufn uf1-uf20
        age	sex	ethnic $ year majorcat major $ politic party bornus	ustime english engtime 
        glasses hand sleep alert compcomf presub comf suspic hypo e1 e2;
  drop bornUS UStime english engtime glasses hand sleep age year alert comfort;
 
/*proc standard mean=0 std=1 out=exp; var bg1-bg20 cw1-cw20 np1-np20 uf1-uf20;run;*/

data compexp; set compexp;
  if bgc = 'a' then do;
     /*bg1-bg20 and cw1-cw20 = pub*/
	 sci_pub1 = bg1; hum_pub1 = bg2; whi_pub1 = bg3; blk_pub1 = bg4; gay_pub1 = bg5; 
     str_pub1 = bg6; rch_pub1 = bg7; por_pub1 = bg8; thn_pub1 = bg9; fat_pub1 = bg10; 
     dem_pub1 = bg11; rep_pub1 = bg12; ins_pub1 = bg13; flw_pub1 = bg14; chr_pub1 = bg15; 
     mus_pub1 = bg16; att_pub1 = bg17; int_pub1 = bg18; evo_pub1 = bg19; cre_pub1 = bg20;
	 sci_pub2 = cw1;  hum_pub2 = cw2;  whi_pub2 = cw3;  blk_pub2 = cw4;  gay_pub2 = cw5; 
     str_pub2 = cw6;  rch_pub2 = cw7;  por_pub2 = cw8;  thn_pub2 = cw9;  fat_pub2 = cw10; 
     dem_pub2 = cw11; rep_pub2 = cw12; ins_pub2 = cw13; flw_pub2 = cw14; chr_pub2 = cw15; 
     mus_pub2 = cw16; att_pub2 = cw17; int_pub2 = cw18; evo_pub2 = cw19; cre_pub2 = cw20;
	 /*np1-np20 and uf1-uf20 = pip*/
	 sci_pip1 = np1;  hum_pip1 = np2;  whi_pip1 = np3;  blk_pip1 = np4;  gay_pip1 = np5; 
     str_pip1 = np6;  rch_pip1 = np7;  por_pip1 = np8;  thn_pip1 = np9;  fat_pip1 = np10; 
     dem_pip1 = np11; rep_pip1 = np12; ins_pip1 = np13; flw_pip1 = np14; chr_pip1 = np15; 
     mus_pip1 = np16; att_pip1 = np17; int_pip1 = np18; evo_pip1 = np19; cre_pip1 = np20;
	 sci_pip2 = uf1;  hum_pip2 = uf2;  whi_pip2 = uf3;  blk_pip2 = uf4;  gay_pip2 = uf5; 
     str_pip2 = uf6;  rch_pip2 = uf7;  por_pip2 = uf8;  thn_pip2 = uf9;  fat_pip2 = uf10; 
     dem_pip2 = uf11; rep_pip2 = uf12; ins_pip2 = uf13; flw_pip2 = uf14; chr_pip2 = uf15; 
     mus_pip2 = uf16; att_pip2 = uf17; int_pip2 = uf18; evo_pip2 = uf19; cre_pip2 = uf20;
  end;
  else do;
     /*bg1-bg20 and cw1-cw20 = pip*/
	 sci_pip1 = bg1;  hum_pip1 = bg2;  whi_pip1 = bg3;  blk_pip1 = bg4;  gay_pip1 = bg5; 
     str_pip1 = bg6;  rch_pip1 = bg7;  por_pip1 = bg8;  thn_pip1 = bg9;  fat_pip1 = bg10; 
     dem_pip1 = bg11; rep_pip1 = bg12; ins_pip1 = bg13; flw_pip1 = bg14; chr_pip1 = bg15; 
     mus_pip1 = bg16; att_pip1 = bg17; int_pip1 = bg18; evo_pip1 = bg19; cre_pip1 = bg20;
	 sci_pip2 = cw1;  hum_pip2 = cw2;  whi_pip2 = cw3;  blk_pip2 = cw4;  gay_pip2 = cw5; 
     str_pip2 = cw6;  rch_pip2 = cw7;  por_pip2 = cw8;  thn_pip2 = cw9;  fat_pip2 = cw10; 
     dem_pip2 = cw11; rep_pip2 = cw12; ins_pip2 = cw13; flw_pip2 = cw14; chr_pip2 = cw15; 
     mus_pip2 = cw16; att_pip2 = cw17; int_pip2 = cw18; evo_pip2 = cw19; cre_pip2 = cw20;
	 /*np1-np20 and uf1-uf20 = pub*/
	 sci_pub1 = np1;  hum_pub1 = np2;  whi_pub1 = np3;  blk_pub1 = np4;  gay_pub1 = np5; 
     str_pub1 = np6;  rch_pub1 = np7;  por_pub1 = np8;  thn_pub1 = np9;  fat_pub1 = np10; 
     dem_pub1 = np11; rep_pub1 = np12; ins_pub1 = np13; flw_pub1 = np14; chr_pub1 = np15; 
     mus_pub1 = np16; att_pub1 = np17; int_pub1 = np18; evo_pub1 = np19; cre_pub1 = np20;
	 sci_pub2 = uf1;  hum_pub2 = uf2;  whi_pub2 = uf3;  blk_pub2 = uf4;  gay_pub2 = uf5; 
     str_pub2 = uf6;  rch_pub2 = uf7;  por_pub2 = uf8;  thn_pub2 = uf9;  fat_pub2 = uf10; 
     dem_pub2 = uf11; rep_pub2 = uf12; ins_pub2 = uf13; flw_pub2 = uf14; chr_pub2 = uf15; 
     mus_pub2 = uf16; att_pub2 = uf17; int_pub2 = uf18; evo_pub2 = uf19; cre_pub2 = uf20;
  end;
     sci_pub = mean(sci_pub1, sci_pub2);  hum_pub = mean(hum_pub1, hum_pub2);  
     whi_pub = mean(whi_pub1, whi_pub2);  blk_pub = mean(blk_pub1, blk_pub2);  
     gay_pub = mean(gay_pub1, gay_pub2);  str_pub = mean(str_pub1, str_pub2);  
     rch_pub = mean(rch_pub1, rch_pub2);  por_pub = mean(por_pub1, por_pub2);  
     thn_pub = mean(thn_pub1, thn_pub2);  fat_pub = mean(fat_pub1, fat_pub2); 
     dem_pub = mean(dem_pub1, dem_pub2);  rep_pub = mean(rep_pub1, rep_pub2); 
     ins_pub = mean(ins_pub1, ins_pub2);  flw_pub = mean(flw_pub1, flw_pub2); 
     chr_pub = mean(chr_pub1, chr_pub2);  mus_pub = mean(mus_pub1, mus_pub2); 
     att_pub = mean(att_pub1, att_pub2);  int_pub = mean(int_pub1, int_pub2); 
     evo_pub = mean(evo_pub1, evo_pub2);  cre_pub = mean(cre_pub1, cre_pub2);
     sci_pip = mean(sci_pip1, sci_pip2);  hum_pip = mean(hum_pip1, hum_pip2);  
     whi_pip = mean(whi_pip1, whi_pip2);  blk_pip = mean(blk_pip1, blk_pip2);  
     gay_pip = mean(gay_pip1, gay_pip2);  str_pip = mean(str_pip1, str_pip2);  
     rch_pip = mean(rch_pip1, rch_pip2);  por_pip = mean(por_pip1, por_pip2);  
     thn_pip = mean(thn_pip1, thn_pip2);  fat_pip = mean(fat_pip1, fat_pip2); 
     dem_pip = mean(dem_pip1, dem_pip2);  rep_pip = mean(rep_pip1, rep_pip2); 
     ins_pip = mean(ins_pip1, ins_pip2);  flw_pip = mean(flw_pip1, flw_pip2); 
     chr_pip = mean(chr_pip1, chr_pip2);  mus_pip = mean(mus_pip1, mus_pip2); 
     att_pip = mean(att_pip1, att_pip2);  int_pip = mean(int_pip1, int_pip2); 
     evo_pip = mean(evo_pip1, evo_pip2);  cre_pip = mean(cre_pip1, cre_pip2);
     schm_pub = sci_pub - hum_pub;  whbk_pub = whi_pub - blk_pub;
	 gyst_pub = gay_pub - str_pub;  rhpr_pub = rch_pub - por_pub;
	 thft_pub = thn_pub - fat_pub;  dmrp_pub = dem_pub - rep_pub;
	 infl_pub = ins_pub - flw_pub;  chms_pub = chr_pub - mus_pub;
	 atin_pub = att_pub - int_pub;  crev_pub = cre_pub - evo_pub;
     schm_pip = sci_pip - hum_pip;  whbk_pip = whi_pip - blk_pip;
	 gyst_pip = gay_pip - str_pip;  rhpr_pip = rch_pip - por_pip;
	 thft_pip = thn_pip - fat_pip;  dmrp_pip = dem_pip - rep_pip;
	 infl_pip = ins_pip - flw_pip;  chms_pip = chr_pip - mus_pip;
	 atin_pip = att_pip - int_pip;  crev_pip = cre_pip - evo_pip;
	 schm_exp = mean(schm_pub, schm_pip); whbk_exp = mean(whbk_pub, whbk_pip);
	 gyst_exp = mean(gyst_pub, gyst_pip); dmrp_exp = mean(dmrp_pub, dmrp_pip);
	 infl_exp = mean(infl_pub, infl_pip); chms_exp = mean(chms_pub, chms_pip);
	 atin_exp = mean(atin_pub, atin_pip); crev_exp = mean(crev_pub, crev_pip);
	 rhpr_exp = mean(rhpr_pub, rhpr_pip); thft_exp = mean(thft_pub, thft_pip);

     DRexp1 = dem_pip1 - rep_pip1;SHexp1 = hum_pip1 - sci_pip1;
     GSexp1 = str_pip1 - gay_pip1;BWexp1 = whi_pip1 - blk_pip1;
	 FIexp1 = flw_pip1 - ins_pip1;CEexp1 = cre_pip1 - evo_pip1;
	 TFexp1 = thn_pip1 - fat_pip1;
     DRexp2 = dem_pip2 - rep_pip2;SHexp2 = hum_pip2 - sci_pip2;
     GSexp2 = str_pip2 - gay_pip2;BWexp2 = whi_pip2 - blk_pip2;
	 FIexp2 = flw_pip2 - ins_pip2;CEexp2 = cre_pip2 - evo_pip2;
	 TFexp2 = thn_pip2 - fat_pip2;

     DRexp = mean(DRexp1, DRexp2);SHexp = mean(SHexp1, SHexp2);
     GSexp = mean(GSexp1, GSexp2);BWexp = mean(BWexp1, BWexp2);
	 FIexp = mean(FIexp1, FIexp2);CEexp = mean(CEexp1, CEexp2);
	 TFexp = mean(TFexp1, TFexp2);

     drop bg1-bg20 cw1-cw20 np1-np20 uf1-uf20;

	 proc sort; by sub;

data study.total; merge compexp study.iFIclean study.iDRclean study.iGSclean study.iBWclean study.iSHclean
                        study.iCEclean study.iTFclean; by sub;

proc means;run;

*implicit multi-trait;
proc corr; var FIiat DRiat SHiat GSiat BWiat CEiat TFiat;run;

*implicit internal consistency;
proc corr; var FIiat1 DRiat1 SHiat1 GSiat1 BWiat1 CEiat1 TFiat1;
           with FIiat2 DRiat2 SHiat2 GSiat2 BWiat2 CEiat2 TFiat2;run;

*explicit multi-trait;
proc corr; var FIexp DRexp SHexp GSexp BWexp CEexp TFexp;run;

*explicit internal consistency;
proc corr; var FIexp1 DRexp1 SHexp1 GSexp1 BWexp1 CEexp1 TFexp1; 
           with FIexp2 DRexp2 SHexp2 GSexp2 BWexp2 CEexp2 TFexp2;run;

*implicit-explicit comparison;
proc corr; var FIiat DRiat SHiat GSiat BWiat CEiat TFiat;
           with FIexp DRexp SHexp GSexp BWexp CEexp TFexp;run;



/*proc print; var sub SHiatexcl BWiatexcl GSiatexcl DRiatexcl;run;
proc means;run;*/

/*proc means; var sc_pvt hm_pvt gy_pvt sp_pvt aa_pvt wh_pvt dm_pvt rp_pvt baa_pvt bdm_pvt bhm_pvt bsc_pvt bgy_pvt;run;
proc corr; var sc_pvt hm_pvt gy_pvt sp_pvt aa_pvt wh_pvt dm_pvt rp_pvt;run;
proc corr; var baa_pvt bdm_pvt bhm_pvt bsc_pvt bgy_pvt;run;
proc corr; var sc_pvt hm_pvt gy_pvt sp_pvt aa_pvt wh_pvt dm_pvt rp_pvt;
           with baa_pvt bdm_pvt bhm_pvt bsc_pvt bgy_pvt;run;*/